var baseUrl = require('../../utils/urllink.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    radioItems: [
      { name: '男', value: '1' },
      { name: '女', value: '2', checked: true }
    ],
    showTopTips: false,
    showTopTipsText: '',
    sex: ''
  },

  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value);

    var radioItems = this.data.radioItems;
    var sex = e.detail.value;
    for (var i = 0, len = radioItems.length; i < len; ++i) {
      radioItems[i].checked = radioItems[i].value == e.detail.value;
    }

    this.setData({
      radioItems: radioItems,
      sex:sex,
    });
    console.log(sex)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    //请求服务器
    wx.request({
      url: baseUrl +'weapp/Users/info.html',
      data: {
        user_id: app.globalData.user_id,
      },
      method: 'GET',
      header: {
        'content-type': 'application/json'
      }, // 设置请求的 header
      success: function (res) {
        console.log(res.data)
        that.setData({
          userInfo: res.data,
          sex: res.data.sex,
        })
        if(res.data.sex > 0){
          var radioItems = that.data.radioItems;
          var sex = res.data.sex;
          for (var i = 0, len = radioItems.length; i < len; ++i) {
            radioItems[i].checked = radioItems[i].value == res.data.sex;
          }

          that.setData({
            radioItems: radioItems,
            sex: sex,
          });
        }



      },
      fail: function () {
      },
      complete: function () {
        // complete
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  formSubmit: function (e) {
    var that = this;
    var sex = e.detail.value.sex;
    if (sex <= 0) {
      this.setData({
        showTopTipsText: '请选择性别',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else {
      //请求服务器
      wx.request({
        url: baseUrl +'weapp/Users/modi_users.html',
        data: {
          user_id: app.globalData.user_id,
          sex: that.data.sex,

        },
        method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        header: {
          'content-type': 'application/json'
        }, // 设置请求的 header
        success: function (res) {
          console.log(res.data)
          if (res.data.code == 0) {
            wx.showModal({
              title: '提示',
              content: '修改成功',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                  wx.switchTab({
                    url: '../mine/mine',
                  })
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })

          } else {
            console.log("修改错误")
            wx.showToast({
              title: '修改错误',
            })
          }
        },
        fail: function () {
        },
        complete: function () {
        }
      })
    }


  },



})